<!--
  speedy-vision.js
  GPU-accelerated Computer Vision for JavaScript
  Copyright 2020-2024 Alexandre Martins <alemartf(at)gmail.com>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  image-features.html
  Feature detection in an image
-->
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="speedy-vision.js: GPU-accelerated Computer Vision for JavaScript">
        <meta name="author" content="Alexandre Martins">
        <title>Speedy feature detection in an image</title>
        <link href="demo-base.css" rel="stylesheet">
        <script src="demo-base.js"></script>
        <script src="../dist/speedy-vision.js"></script>
    </head>
    <body>
        <h1>Finding features in an image</h1>
        <form autocomplete="off">
            <div>
                <label for="sensitivity">Sensitivity</label>
                <input type="range" min="0.0" max="0.99" value="0.5" step="0.01" id="sensitivity">
            </div>
            <div>
                <label for="multiscale">Multi-scale</label>
                <input type="checkbox" id="multiscale">
            </div>
        </form>
        <div>
            <span id="status"></span>
            <canvas id="canvas-demo"></canvas><br>
        </div>
        <img id="image" src="../assets/masp.jpg" title="Free photo by ckturistando (pexels.com)" hidden>
        <script>
window.onload = async function()
{
    // form components
    const sensitivity = document.getElementById('sensitivity');
    const multiscale = document.getElementById('multiscale');

    // tell Speedy to load the image
    const image = document.getElementById('image');
    const media = await Speedy.load(image);

    // create the pipeline
    const pipeline = Speedy.Pipeline();
    const source = Speedy.Image.Source();
    const greyscale = Speedy.Filter.Greyscale();
    const pyramid = Speedy.Image.Pyramid();
    const harris = Speedy.Keypoint.Detector.Harris();
    const sink = Speedy.Keypoint.Sink();

    source.media = media;
    harris.capacity = 8192;

    source.output().connectTo(greyscale.input());
    greyscale.output().connectTo(pyramid.input());
    pyramid.output().connectTo(harris.input());
    harris.output().connectTo(sink.input());

    pipeline.init(source, greyscale, pyramid, harris, sink);

    // find & render features
    const canvas = setupCanvas('canvas-demo', media.width, media.height, image.title);
    async function update()
    {
        // multiscale?
        harris.levels = multiscale.checked ? 8 : 1;

        // adjust sensitivity
        harris.quality = 1 - sensitivity.value;

        // find features
        const { keypoints } = await pipeline.run();

        // render features
        renderMedia(media, canvas);
        renderKeypoints(canvas, keypoints);
        renderStatus(keypoints);
    }
    update();

    // recalculate feature points
    // when changing the controls
    sensitivity.oninput = update;
    multiscale.oninput = update;
}
        </script>
        <section id="speedy-vision">
            <span>Powered by <a href="https://github.com/alemart/speedy-vision" target="_blank">speedy-vision.js</a></span>
            <a href="https://github.com/sponsors/alemart" target="_blank" class="button">
                <img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/alemart?style=for-the-badge&logo=github&label=Sponsor&labelColor=royalblue&color=gold">
            </a>
        </section>
    </body>
</html>